var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
// var logger = require('morgan');
let intercept = require('./tools/intercept');

var indexRouter = require('./routes/index');
// var usersRouter = require('./routes/users');
var listRouter = require('./routes/list');
var courseRouter = require('./routes/course')
let memberRouter = require('./routes/member')

var app = express();

// app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use("/uploads",express.static(path.join(__dirname, 'uploads')));

app.use((req,res,next)=>{
    res.header('Access-Control-Allow-Origin','*');
    res.header('Access-Control-Allow-Headers','token');
    res.header('Access-Control-Allow-Methods','put,delete,post');
    next();
})

app.use('/apiv1', indexRouter);
app.use('/apiv1/list',listRouter);
app.use('/apiv1/course',courseRouter);
app.use('/apiv1/member',memberRouter)

//验证jwt中间件，需要验证的接口在下面，不需要拦截的在上面
app.use(intercept());

//自定义错误消息中间件
// app.use((err,req,res,next)=>{
//     res.json({code:500,data:err,msg:'错误'});
//     // next();
// })
//404 错误处理中间件
app.use((req,res)=>{
    res.json({code:404,data:null,msg:'未找到资源'})
})

module.exports = app;
